import java.util.ArrayList;

public class Player {
	private String name;
	private int money;
	private Token token;
	private boolean isJail=false;
	private int ownedA = -1;
	private int ownedU = -1;
	private int Pair=0;
	@SuppressWarnings({"unchecked"})
	private ArrayList[] listOfColors;
	private Integer[] colorGroup = new Integer[8];
	private int ownedHouse = 0;
	private Boolean[] canBuildOn,canSellOn,canMortageOn;
	
	public Player(){
		canBuildOn = new Boolean[8];
		for (int i = 0; i < 8; i++) {
			canBuildOn[i] = new Boolean(false);
		}
		canSellOn = new Boolean[8];
		for (int i = 0; i < 8; i++) {
			canSellOn[i] = new Boolean(false);
		}
		canMortageOn = new Boolean[8];
		for (int i = 0; i < 8; i++) {
			canMortageOn[i] = new Boolean(false);
		}
		listOfColors = new ArrayList[10];
		listOfColors[0] = new ArrayList<Block>();
		listOfColors[1] = new ArrayList<Block>();
		listOfColors[2] = new ArrayList<Block>();
		listOfColors[3] = new ArrayList<Block>();
		listOfColors[4] = new ArrayList<Block>();
		listOfColors[5] = new ArrayList<Block>();
		listOfColors[6] = new ArrayList<Block>();
		listOfColors[7] = new ArrayList<Block>();
		listOfColors[8] = new ArrayList<Block>();
		listOfColors[9] = new ArrayList<Block>();
		colorGroup[0] = 2;
		colorGroup[1] = 3;
		colorGroup[2] = 3;
		colorGroup[3] = 3;
		colorGroup[4] = 3;
		colorGroup[5] = 3;
		colorGroup[6] = 3;
		colorGroup[7] = 2;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public int getMoney() {
		return money;
	}

	public void setToken(Token token) {
		this.token = token;
	}

	public Token getToken() {
		return token;
	}
	
	public void createToken(String tokenName, int sPoint){
		token = new Token(tokenName,sPoint);
	}
	
	@SuppressWarnings("unchecked")
	public void buyProperty(Block property){
		property.getTitle(this.getToken().getLocation()).setAvailable(false);
		property.getTitle(this.getToken().getLocation()).setOwner(this.getName());
		this.setMoney(this.getMoney()-property.getTitle(this.getToken().getLocation()).getPrice());
		if(property.getTitle(this.getToken().getLocation()) == property.getAirport()){
			property.getTitle(this.getToken().getLocation());
			listOfColors[8].add(property.getTitle(this.getToken().getLocation()));
			this.ownedA++;
		}
		else if(property.getTitle(this.getToken().getLocation()) == property.getUtility()){
			property.getTitle(this.getToken().getLocation());
			listOfColors[9].add(property.getTitle(this.getToken().getLocation()));
			this.ownedU++;
		}
		else{
			if(property.getTitle(this.getToken().getLocation()).getColor().equals("brown")){
				listOfColors[0].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("ltblue")){
				listOfColors[1].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("pink")){
				listOfColors[2].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("orange")){
				listOfColors[3].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("red")){
				listOfColors[4].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("yellow")){
				listOfColors[5].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("green")){
				listOfColors[6].add(property.getTitle(this.getToken().getLocation()));				
			}
			else if(property.getTitle(this.getToken().getLocation()).getColor().equals("blue")){
				listOfColors[7].add(property.getTitle(this.getToken().getLocation()));				
			}
		}
		for (int i = 0; i < 8; i++) {
			if(listOfColors[i].size() == colorGroup[i]){
				this.setOwnedHouse(getOwnedHouse()+1);
				property.getTitle(this.getToken().getLocation()).setCanBuild(true);
				setCanBuildOn(true, i);
			}
		}
	}
	
	public void payRent(Block property, String owner, Player[] player, Integer[] orderTurn, int value){
		if(!(this.getName().equals(property.getTitle(this.getToken().getLocation()).getOwner()))){
			for (int i = 0; i < orderTurn.length; i++) {
				if(player[orderTurn[i]].getName() == owner){
					int payment=0;
					if(property.getTitle(this.token.getLocation()) == property.getAirport()){
						payment = property.getTitle(this.token.getLocation()).getPayment(player[orderTurn[i]].getOwnedA());
					}
					else if(property.getTitle(this.token.getLocation()) == property.getUtility()){
						payment = value*property.getTitle(this.token.getLocation()).getPayment(player[orderTurn[i]].getOwnedU());
					}
					else
						payment = property.getTitle(this.token.getLocation()).getPayment(player[orderTurn[i]].getOwnedHouse());
					player[orderTurn[i]].setMoney(player[orderTurn[i]].getMoney() + payment);
					this.setMoney(this.getMoney() - payment);
				}
			}
		}
	}
	
	public void build(){
		
	}
	
	public void sell(){
		
	}

	public void setJail(boolean isJail) {
		this.isJail = isJail;
	}

	public boolean isJail() {
		return isJail;
	}

	public void setOwnedA(int ownedA) {
		this.ownedA = ownedA;
	}

	public int getOwnedA() {
		return ownedA;
	}

	public void setOwnedU(int ownedU) {
		this.ownedU = ownedU;
	}

	public int getOwnedU() {
		return ownedU;
	}

	public void setPair(int pair) {
		Pair = pair;
	}

	public int getPair() {
		return Pair;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Block> getListOfColors(int array) {
		return listOfColors[array];
	}

	public void setOwnedHouse(int ownedHouse) {
		this.ownedHouse = ownedHouse;
	}

	public int getOwnedHouse() {
		return ownedHouse;
	}

	public void setCanBuildOn(Boolean canBuildOn, int index) {
		this.canBuildOn[index] = canBuildOn;
	}

	public Boolean getCanBuildOn(int index) {
		return canBuildOn[index];
	}

	public void setCanSellOn(Boolean canSellOn, int index) {
		this.canBuildOn[index] = canSellOn;
	}

	public Boolean getCanSellOn(int index) {
		return canBuildOn[index];
	}
	
	public void setCanMortageOn(Boolean canMortageOn, int index) {
		this.canMortageOn[index] = canMortageOn;
	}

	public Boolean getCanMortageOn(int index) {
		return canMortageOn[index];
	}
	
}